package agt.game.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback {

	private RenderThread thread_ = null;
	private Bitmap bitmap_;
	
	public GameSurface(Context context) {
		super(context);
		
		Bitmap btSrc = BitmapFactory.decodeResource(getResources(), R.drawable.bt);
		bitmap_ = Bitmap.createBitmap(btSrc.getWidth(), btSrc.getHeight(), Bitmap.Config.ARGB_8888);
	
		int bw = bitmap_.getWidth();
		int bh = bitmap_.getHeight();
		int[] pixels = new int [bw * bh];
		btSrc.getPixels(pixels, 0, bw, 0, 0, bw, bh);
		bitmap_.setPixels(pixels, 0, bw, 0, 0, bw, bh);
		
		int c;
		for (int i = 0; i < bw; i++) {
			for (int j = 0; j < bh; j++) {
				c = bitmap_.getPixel(i, j);
				if (c == 0xffff00ff) {
					c = 0x00ffffff;
				}
				bitmap_.setPixel(i, j, c);
			}
		}
		
		getHolder().addCallback(this);
		thread_ = new RenderThread(this);
	}
	
	@Override
	public void draw(Canvas _canvas) {
		_canvas.drawColor(Color.BLUE);
		_canvas.drawBitmap(bitmap_, new Rect(0, 0, 32, 32), new Rect(10, 10, 42, 42), null);
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		if (thread_ != null)	{
			thread_.setRunning(true);
			thread_.start();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		if (thread_ != null) {
			thread_.setRunning(false);
		}
	}
}
